package com.luck.easyfoodbatch.pickerview;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.util.Tools;

import java.util.Calendar;
import java.util.List;


public class PopAlarmHelper {

    private Context context;
    private PopupWindow pop;
    private View view;
    private OnClickOkListener onClickOkListener;

    private List<String> listHour, ListPeriod, listMinute,
            list24Time;
    private String year, hour, minute, stage;

    public PopAlarmHelper(Context context) {
        this.context = context;
        view = ((LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
                R.layout.alarm_date, null);
        pop = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        initPop();
        initData();
        initView();
    }

    private void initPop() {
        pop.setAnimationStyle(android.R.style.Animation_InputMethod);
        pop.setFocusable(true);
        pop.setOutsideTouchable(true);
        pop.setBackgroundDrawable(new BitmapDrawable());
        pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    }

    /**
     * 初始化数据
     */
    private void initData() {
        listHour = DatePackerUtil.getHourList();
        listMinute = DatePackerUtil.getMinuteList();
        ListPeriod = DatePackerUtil.getPeriodAllList();// 上下午
        list24Time = DatePackerUtil.getTime24AllList();

    }

    boolean b = false;
    boolean cb = false;// 是否没有滚动 第一次选时间
    private int position = 0;

    private void initView() {
        final Calendar c = Calendar.getInstance();// 当前时间
        Button btnCancel = (Button) view.findViewById(R.id.btnCancel);
        Button btnOk = (Button) view.findViewById(R.id.btnOK);
        final LoopView loopView1 = (LoopView) view.findViewById(R.id.loopView1);
        final LoopView loopView2 = (LoopView) view.findViewById(R.id.loopView2);
        final LoopView loopView3 = (LoopView) view.findViewById(R.id.loopView3);

        int apm = c.get(Calendar.AM_PM);
        loopView1.setList(ListPeriod);
        loopView1.setNotLoop();
        //apm=0 表示上午，apm=1表示下午。
        switch (apm) {
            case 0:
                loopView1.setCurrentItem(0);
                loopView2.setList(listHour);
                break;
            case 1:
                loopView1.setCurrentItem(1);
                loopView2.setList(list24Time);
                break;
        }

        loopView2.setNotLoop();
        loopView2.setCurrentItem(0);

        loopView3.setList(listMinute);
        loopView3.setNotLoop();
        loopView3.setCurrentItem(0);


        year = c.get(Calendar.YEAR) + "";
        loopView1.setListener(new LoopListener() {
            @Override
            public void onItemSelect(int item) {
                if (b == false) {
                    int apm = c.get(Calendar.AM_PM);
                    //apm=0 表示上午，apm=1表示下午。
                    switch (apm) {
                        case 0:
                            stage = ListPeriod.get(0);
                            loopView2.setList(listHour);
                            break;
                        case 1:
                            stage = ListPeriod.get(1);
                            loopView2.setList(list24Time);
                            break;
                    }

                    loopView2.setNotLoop();
                    loopView2.setCurrentItem(0);
                    b = true;
                } else {
                    stage = ListPeriod.get(item);
                    if (stage.equals("上午")) {
                        loopView2.setList(listHour);
                        loopView2.setNotLoop();
                        loopView2.setCurrentItem(0);
                    } else {
                        loopView2.setList(list24Time);
                        loopView2.setNotLoop();
                        loopView2.setCurrentItem(0);
                    }
                }

            }
        });

        loopView2.setListener(new LoopListener() {
            @Override
            public void onItemSelect(int item) {
                if (Tools.isNull(stage)) {
                    stage = "上午";
                }
                if (stage.equals("上午")) {
                    hour = listHour.get(item);
                } else {
                    hour = list24Time.get(item);
                }
                position = item;
            }
        });

        loopView3.setListener(new LoopListener() {
            @Override
            public void onItemSelect(int item) {
                if (!cb) {
                    minute = listMinute.get(0);
                    cb = true;
                } else {
                    minute = listMinute.get(item);
                }
            }
        });


        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.dismiss();
            }
        });

        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.dismiss();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        onClickOkListener.onClickOk(position, year, stage, hour, minute);
                    }
                }, 100);
            }
        });
    }

    /**
     * 显示
     *
     * @param view
     */
    public void show(View view) {
        pop.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }

    /**
     * 隐藏监听
     *
     * @param onDismissListener
     */
    public void setOnDismissListener(
            PopupWindow.OnDismissListener onDismissListener) {
        pop.setOnDismissListener(onDismissListener);
    }

    public void setOnClickOkListener(OnClickOkListener onClickOkListener) {
        this.onClickOkListener = onClickOkListener;
    }

    public interface OnClickOkListener {
        public void onClickOk(int position, String year, String stage, String date, String time);
    }

}
